//package com.game.gate;
//
//import java.util.HashMap;
//import java.util.Map;
//
//import com.game.cluster.ClusterClient;
//import com.game.cluster.ClusterMessage;
//import com.game.cluster.ClusterSystem;
//import com.game.net.ConnectListener;
//import com.game.net.Inbox;
//import com.game.netty.NettyConnect;
//import com.game.protobuf.MessageUtil;
//import com.game.protobuf.PFMessage;
//import com.game.protobuf.PingMessageHandler;
//
//import com.game.message.SessionCreate;
//import com.game.message.SessionLogout;
//import com.game.message.SessionQuit;
//import com.game.net.Connect;
//import com.game.net.Inbox;
//import com.game.net.NetAddress;
//import com.game.protobuf.ProtostuffUtil;
//import org.alan.mars.tips.MarsResultEnum;
//import org.alan.mars.tips.ServerTips;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//public class GateSession extends NettyConnect<PFMessage> implements Inbox<PFMessage>, ConnectListener {
//    public static Map<String, GateSession> gateSessionMap = new HashMap<>();
//
//    protected Logger log = LoggerFactory.getLogger(getClass());
//
//    private ClusterClient currentClient;
//
//    public String sessionId;
//
//    public long userId;
//
//    public boolean certify;
//
//    public long activeTime;
//
//    public long createTime;
//
//    public Connect connect;
//
//    public void onClusterReceive(Connect connect, PFMessage msg) {
//        write(msg);
//    }
//
//    public void messageReceived(PFMessage msg) {
//        if (msg.messageType == 150 && msg.cmd == 152) {
//            checkServer();
//            return;
//        }
//        if (!this.certify && msg.messageType != 1000) {
//            this.log.warn("未认证消息错误{}", msg);
//            close();
//            return;
//        }
//        if (msg.messageType == 1 && msg.cmd == 1) {
//            this.activeTime = System.currentTimeMillis();
//            PFMessage pfMessage = new PFMessage(1, 2, ProtostuffUtil.serialize(new PingMessageHandler.Pong(this.activeTime)));
//            write(pfMessage);
//            return;
//        }
//        ClusterMessage clusterMessage = new ClusterMessage(this.sessionId, msg, this.userId);
//        try {
//            Connect micconnect = ClusterSystem.system.micserviceAllot(this.currentClient, msg.messageType);
//            if (micconnect != null) {
//                micconnect.write(clusterMessage);
//                return;
//            }
//        } catch (Exception e) {
//            this.log.debug("微服务发送消息失败"+ msg.messageType, e);
//        }
//        if (this.connect == null || !this.connect.isActive()) {
//            if (this.connect != null)
//                this.currentClient.close(this.connect);
//            this.connect = getConnect();
//        }
//        this.connect.write(clusterMessage);
//    }
//
//    public void onClose() {
//        this.log.debug("{}", this.sessionId);
//        gateSessionMap.remove(this.sessionId);
//        sendClose();
//        if (this.userId > 0L)
//            sendLogout();
//        if (this.connect != null)
//            this.connect.removeConnectListener(this);
//        this.currentClient = null;
//        this.connect = null;
//    }
//
//    public void sendLogout() {
//        try {
//            SessionLogout sessionLogout = new SessionLogout();
//            sessionLogout.sessionId = this.sessionId;
//            sessionLogout.playerId = this.userId;
//            PFMessage pfMessage = MessageUtil.getPFMessage(sessionLogout);
//            ClusterMessage clusterMessage = new ClusterMessage(this.sessionId, pfMessage, this.userId);
//            ClusterClient clusterClient = ClusterSystem.system.getByNodeType(NodeType.ACCOUNT, this.remoteAddress.getHost(), this.userId);
//            if (this.currentClient != null)
//                clusterClient.getConnect().write(clusterMessage);
//        } catch (Exception e) {
//            this.log.warn("用户下线消息发送异常", e);
//        }
//    }
//
//    private void sendClose() {
//        if (this.connect == null || this.currentClient == null) {
//            this.log.warn(");
//            return;
//        }
//        SessionQuit sessionQuit = new SessionQuit();
//        sessionQuit.sessionId = this.sessionId;
//        PFMessage pfMessage = MessageUtil.getPFMessage(sessionQuit);
//        ClusterMessage clusterMessage = new ClusterMessage(this.sessionId, pfMessage, this.userId);
//        this.connect.write(clusterMessage);
//    }
//
//    private void sendEnter() {
//        if (this.connect == null || this.currentClient == null) {
//            this.log.warn(");
//            return;
//        }
//        this.log.debug("{},nodeAddress={}", this.currentClient.nodeConfig.getName(), this.currentClient.nodeConfig.getTcpAddress());
//        SessionCreate sessionCreate = new SessionCreate();
//        sessionCreate.sessionId = this.sessionId;
//        sessionCreate.netAddress = this.remoteAddress;
//        sessionCreate.userId = this.userId;
//        sessionCreate.nodePath = ClusterSystem.system.getNodePath();
//        PFMessage pfMessage = MessageUtil.getPFMessage(sessionCreate);
//        ClusterMessage clusterMessage = new ClusterMessage(this.sessionId, pfMessage, this.userId);
//        this.connect.write(clusterMessage);
//    }
//
//    public void onCreate() {
//        this.sessionId = this.ctx.channel().id().asShortText();
//        this.log.debug("{},net={}", this.sessionId, this.remoteAddress);
//        String ip = this.remoteAddress.getHost();
//        this.remoteAddress.setHost(ip);
//    }
//
//    protected void checkServer() {
//        this.log.debug("收到服务检查消息，sessionID={},netAddress={}", this.sessionId, this.remoteAddress);
//        this.activeTime = this.createTime = System.currentTimeMillis();
//        gateSessionMap.put(this.sessionId, this);
//        this.currentClient = ClusterSystem.system.getByNodeType(NodeType.ACCOUNT, this.remoteAddress.getHost(), this.userId);
//        sendServerStatus();
//    }
//
//    public void sendServerStatus() {
//        if (this.currentClient == null) {
//            this.log.debug(");
//                    write(MessageUtil.getPFMessage(new ServerTips(MarsResultEnum.NETWORK_CANT_USE)));
//            return;
//        }
//        this.connect = getConnect();
//        if (this.connect == null) {
//            this.log.debug(");
//                    write(MessageUtil.getPFMessage(new ServerTips(MarsResultEnum.NETWORK_CANT_USE)));
//            return;
//        }
//        sendEnter();
//        write(MessageUtil.getPFMessage(new ServerTips(MarsResultEnum.NETWORK_SUCCESS)));
//    }
//
//    public void switchNode(ClusterClient clusterClient) {
//        this.log.debug("{},userId={},srcPath={},targetPath={},certify={}", new Object[] { this.sessionId, Long.valueOf(this.userId), this.currentClient.nodeConfig.getName(), clusterClient.nodeConfig.getName(), Boolean.valueOf(this.certify) });
//        if (this.certify) {
//            sendClose();
//            this.currentClient = clusterClient;
//            this.connect = getConnect();
//            sendEnter();
//        }
//    }
//
//    public void onKickout() {
//        this.log.info("{}{}", this.sessionId, Long.valueOf(this.userId));
//        this.userId = 0L;
//        try {
//            writeAndClose(MessageUtil.getPFMessage(new ServerTips(MarsResultEnum.PLAYER_KICKOUT)));
//        } catch (Exception e) {
//            this.log.warn(", e);
//                    e.printStackTrace();
//        }
//    }
//
//    public void onConnectClose(Connect connect) {
//        this.log.warn("{},sessionId={},nodeAddress={}", new Object[] { Long.valueOf(this.userId), this.sessionId, connect.address() });
//        try {
//            writeAndClose(MessageUtil.getPFMessage(new ServerTips(MarsResultEnum.NETWORK_CANT_USE)));
//        } catch (Exception e) {
//            this.log.warn(", e);
//                    e.printStackTrace();
//        }
//    }
//
//    public Connect getConnect() {
//        if (this.connect != null)
//            this.connect.removeConnectListener(this);
//        try {
//            this.connect = this.currentClient.getConnectSync();
//            this.connect.addConnectListener(this);
//        } catch (Exception e) {
//            this.log.warn("+ this.currentClient.marsNode.getNodePath(), e);
//                    write(MessageUtil.getPFMessage(new ServerTips(MarsResultEnum.NETWORK_CANT_USE)));
//            gateSessionMap.remove(this.sessionId);
//            close();
//        }
//        return this.connect;
//    }
//
//    public boolean isActive() {
//        long currentTime = System.currentTimeMillis();
//        if (currentTime - this.createTime > 5000L && !this.certify)
//            return false;
//        if (currentTime - this.activeTime > 30000L)
//            return false;
//        return true;
//    }
//
//    public void setHost(String hostIp) {
//        if (hostIp != null && !hostIp.isEmpty())
//            this.remoteAddress = new NetAddress(hostIp, (this.remoteAddress != null) ? this.remoteAddress.getPort() : 0);
//    }
//
//    public String toString() {
//        return "GateSession{sessionId='" + this.sessionId + '\'' + ", userId=" + this.userId + ", certify=" + this.certify + ", connect=" + this.connect + '}';
//    }
//}
//
